package com.hoshiicloud.lessee.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.goods.entity.goods.*;
import com.hoshiicloud.goods.vo.GoodsCategoryEditInfoVo;
import com.hoshiicloud.lessee.feign.LesseeGoodsFeignService;
import com.hoshiicloud.lessee.feign.IdGeneratorService;
import com.hoshiicloud.lessee.service.LesseeGoodCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class LessGoodsCategoryServiceImpl implements LesseeGoodCategoryService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    @Autowired
    private LesseeGoodsFeignService goodFeignService;


    /**
     * 添加商品类目
     * @param goodsCategory
     * @param proIds
     * @param specIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsCategory", allEntries = true)
    @Logable(authName = "商品类目管理-添加类目 ")
    public boolean addGoodsCategory(GoodsCategory goodsCategory, List<Long> proIds, List<Long> specIds) {
        Response<Long> rs = goodFeignService.saveGoodsCategory(goodsCategory);
        goodsCategory.setId(rs.getData());
        this.setGoodsCategoryProperties(proIds,goodsCategory);
        this.setGoodsCategorySpec(specIds,goodsCategory);
        return true;
    }

    /**
     * 修改商品类目
     * @param goodsCategory
     * @param proIds
     * @param specIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "GoodsCategory", allEntries = true)
    @Logable(authName = "商品类目管理-聚合编辑类目 ")
    public boolean updateGoodsCategory(GoodsCategory goodsCategory, List<Long> proIds, List<Long> specIds) {
        goodFeignService.updateGoodsCategory(goodsCategory);
        this.deleteGoodsCategoryPro(goodsCategory.getId());
        this.deleteGoodsCategorySpec(goodsCategory.getId());
        this.setGoodsCategoryProperties(proIds,goodsCategory);
        this.setGoodsCategorySpec(specIds,goodsCategory);
        return true;
    }

    @Override
    @Logable(authName = "商品类目管理-聚合查询 编辑类目时所需信息 ")
    public GoodsCategoryEditInfoVo queryGoodsCategoryEditInfo(Long id) {
        GoodsCategoryEditInfoVo vo = new GoodsCategoryEditInfoVo();
        Response<GoodsCategory> gcData =  goodFeignService.queryGoodsCategoryById(id);
        GoodsCategory gc = gcData.getData();
        Long[] categoryIds ;
        if(gc.getLevel() == 1){
            categoryIds= new Long[]{};
        }else if(gc.getLevel() == 2){
            categoryIds= new Long[]{gc.getParentId()};
        }else{
            Response<GoodsCategory> parentData = goodFeignService.queryGoodsCategoryById(gc.getParentId());
            GoodsCategory parent = parentData.getData();
            categoryIds= new Long[]{parent.getParentId(),gc.getParentId()};
        }
        vo.setIds(categoryIds);
        vo.setId(gc.getId());
        vo.setName(gc.getName());
        vo.setImportTaxes(gc.getImportTaxes());
        vo.setListSpec(this.getGoodsCategorySpec(id));
        vo.setListPro(this.getGoodsCategoryPro(id));
        return vo;
    }


    private List<GoodsSpecifications> getGoodsCategorySpec(Long categoryId){
        Response<List<GoodsSpecifications>> rs = goodFeignService.listGoodsCategorySpecByCategoryId(categoryId);
        return rs.getData() ;
    }


    private List<GoodsProperties> getGoodsCategoryPro(Long categoryId){
        Response<List<GoodsProperties>> rs = goodFeignService.listGoodsCategoryProByCategoryId(categoryId);
        return rs.getData() ;
    }

    private void setGoodsCategoryProperties(List<Long> proIds,GoodsCategory goodsCategory){
        if(proIds.size()> 0 ){
            LocalDateTime now  = LocalDateTime.now();
            List<GoodsCategoryProperties> arry = new ArrayList<>();
            for(Long prodId: proIds){
                GoodsCategoryProperties temp = new GoodsCategoryProperties();
                temp.setId(this.getId());
                temp.setCompanyId(goodsCategory.getCompanyId());
                temp.setCategoryId(goodsCategory.getId());
                temp.setCreatedTime(now);
                temp.setCreatedBy(goodsCategory.getCreatedBy());
                temp.setDeleted(0);
                temp.setPropertiesId(prodId);
                arry.add(temp);
            }
            goodFeignService.saveGoodsCategoryPropertieBatch(arry);
        }
    }
    private void setGoodsCategorySpec(List<Long> specIds,GoodsCategory goodsCategory){
        if(specIds.size()> 0 ){
            LocalDateTime now  = LocalDateTime.now();
            List<GoodsCategorySpec> arry = new ArrayList<>();
            for(Long specId: specIds){
                GoodsCategorySpec temp = new GoodsCategorySpec();
                temp.setId(this.getId());
                temp.setCompanyId(goodsCategory.getCompanyId());
                temp.setCategoryId(goodsCategory.getId());
                temp.setCreatedTime(now);
                temp.setCreatedBy(goodsCategory.getCreatedBy());
                temp.setDeleted(0);
                temp.setSpecId(specId);
                arry.add(temp);
            }
            goodFeignService.saveGoodsCategorySpecBatch(arry);
        }
    }

    private void deleteGoodsCategorySpec(Long categoryId){
        goodFeignService.deleteGoodsCategorySpecByCategoryId(categoryId);
    }
    private void deleteGoodsCategoryPro(Long categoryId){
        goodFeignService.deleteGoodsCategoryPropertieByCategoryId(categoryId);
    }
    /**
     * @Author qry
     * @Date 15:11 2019/8/9
     * ID 生成器
     **/
    private Long getId(){
        String id = idGeneratorService.queryGeneratorId();
        if (StrUtil.isBlank(id)) {
            throw new ServiceException(CommonError.error("主键ID生成失败"));
        }
        return Long.valueOf(id);
    }
}
